﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Data.SqlClient;

namespace QL_NOI_TRU
{
    public partial class frmPhanCongNVPhuTrachPhongBenh : DevExpress.XtraEditors.XtraForm
    {
        public frmPhanCongNVPhuTrachPhongBenh()
        {
            InitializeComponent();
        }

        private void frmPhanCongNVPhuTrachPhongBenh_Load(object sender, EventArgs e)
        {
            // do du lieu vao grid
            var kn = new Ketnoi(frmDangnhap.stringConn);
            gridControl1.DataSource = kn.ExcuteQuery("select NV_PHUTRACH_PB.MA_NV as 'Mã nhân viên', NHAN_VIEN.HOTEN as 'Họ tên',MAPHONG as 'Mã phòng',NGBDAU as 'Ngày bắt đầu',NGKTHUC as 'Ngày kết thúc' from NV_PHUTRACH_PB,NHAN_VIEN where NV_PHUTRACH_PB.MA_NV = NHAN_VIEN.MA_NV");

            // do du lieu vao cbbKhoa
            var a = "select * from KHOA";
            SqlConnection conn = new SqlConnection(frmDangnhap.stringConn);
            SqlCommand cm = conn.CreateCommand();
            cm.CommandText = a;
            conn.Open();
            SqlDataReader dr = cm.ExecuteReader();
            while (dr.Read())
            {
                cbbKhoa.Properties.Items.Add(String.Format("{0} | {1}", dr["MA_KHOA"], dr["TEN_KHOA"]));
            }
            dr.Close();
        }

        private void cbbKhoa_SelectedIndexChanged(object sender, EventArgs e)
        {
            var c = cbbKhoa.Text.Substring(0, 2);
            var a = "select MAPHONG from PHONG_BENH where PHONG_BENH.MA_KHOA = '"+c+"'";
            SqlConnection conn = new SqlConnection(frmDangnhap.stringConn);
            SqlCommand cm = conn.CreateCommand();
            cm.CommandText = a;
            conn.Open();
            SqlDataReader dr = cm.ExecuteReader();
            while (dr.Read())
            {
                cbbPhong.Properties.Items.Add(String.Format("{0}", dr["MAPHONG"]));
            }
        }

        private void cbbPhong_SelectedIndexChanged(object sender, EventArgs e)
        {
            var a = "select MA_NV,HOTEN from NHAN_VIEN";
            SqlConnection conn = new SqlConnection(frmDangnhap.stringConn);
            SqlCommand cm = conn.CreateCommand();
            cm.CommandText = a;
            conn.Open();
            SqlDataReader dr = cm.ExecuteReader();
            while (dr.Read())
            {
                cbbNhanVien.Properties.Items.Add(String.Format("{0} | {1}", dr["MA_NV"], dr["HOTEN"]));
            }
        }

        private void btRefresh_Click(object sender, EventArgs e)
        {
            cbbKhoa.Text = "";
            cbbPhong.Text = "";
            cbbNhanVien.Text = "";
            cbbNgayBD.Text = "";
            cbbNgayKT.Text = "";
            frmPhanCongNVPhuTrachPhongBenh_Load(null, null);
        }
        /// <summary>
        /// Lưu dữ liệu
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btLuu_Click(object sender, EventArgs e)
        {
            string strr = "EXAC Insert_NV_PHUTRACH_PB @MA_NV = '" + cbbNhanVien.Text.Substring(0, 10).Trim() + "',@MAPHONG = '" + cbbPhong.Text.Substring(0, 4).Trim() + "',@NGBDAU = '" + cbbNgayBD.Text.ToString().Trim() + "',@NGKTHUC='" + cbbNgayKT.Text.ToString().Trim() + "'";
            var kn = new Ketnoi(frmDangnhap.stringConn);
            kn.ExcuteQuery(strr);
            btRefresh_Click(null, null);
        }




        private void cbbKhoa_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
                cbbPhong.Focus();
        }

        private void cbbPhong_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
                cbbNhanVien.Focus();
        }

        private void cbbNhanVien_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
                cbbNgayBD.Focus();
        }

        private void cbbNgayBD_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
                cbbNgayKT.Focus();
        }

        private void cbbNgayKT_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
                btLuu.Focus();
        }

    }
}